//
//  CinemaViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/18.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "CinemaViewController.h"
#import "JWDistrictModel.h"
#import "JWCinemaModel.h"
#import "JWCinemaCell.h"
#import "constant.h"

@interface CinemaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray *districtAr;
@property (nonatomic, strong) NSArray *cinemaAr;
@property (nonatomic, strong) NSArray *allCinemasAr;
@property (nonatomic, strong) NSMutableArray *closedAr;//关闭的数组
@property (nonatomic, strong) UITableView *mTableView;

@end

@implementation CinemaViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"影院";
        self.navigationController.navigationBar.translucent = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.closedAr = [NSMutableArray array];
    [self loadTabelView];
    
}

#pragma mark - 加载和整理文件

- (NSArray *)districtAr{
    
    NSMutableDictionary *dic1 = [self objectWithJsonName:@"district_list"];

    NSArray *listAr1 = [dic1 objectForKey:@"districtList"];
    
    //    城区
    
    if (!_districtAr) {
        NSMutableArray *tempAr1 = [NSMutableArray array];
        
        for (int i = 0; i < listAr1.count; i++) {
            NSDictionary *districtInfoDic = listAr1[i];
            
            JWDistrictModel *districtmodel = [JWDistrictModel districtModelWithDictionary:districtInfoDic];
            [tempAr1 addObject:districtmodel];
            
        }
        _districtAr = tempAr1;
    }
    
    return _districtAr;
}

- (NSArray *)cinemaAr{

    NSMutableDictionary *dic2 = [self objectWithJsonName:@"cinema_list"];
    NSArray *listAr2 = [dic2 objectForKey:@"cinemaList"];
    
    //    影院
    
    if (!_cinemaAr) {
        NSMutableArray *tempAr2 = [NSMutableArray array];
        
        for (int i = 0; i < listAr2.count; i++) {
            NSDictionary *cinemaInfoDic = listAr2[i];
            JWCinemaModel *cinemaModel = [JWCinemaModel cinemaModelWithDictionary:cinemaInfoDic];
            [tempAr2 addObject:cinemaModel];
        }
        _cinemaAr = tempAr2;
    }
    return _cinemaAr;
}

- (NSArray *)allCinemasAr{
    
    if (!_allCinemasAr) {
        
        NSMutableArray *districts = [NSMutableArray array];
        
        for (JWDistrictModel *districtModel in self.districtAr) {
            
            NSString *idStr = districtModel.id;
            NSMutableArray *cinemas = [NSMutableArray array];
            
            for (JWCinemaModel *cinemaModel in self.cinemaAr) {
                NSString *districtIdStr = cinemaModel.districtId;
                if ([idStr isEqualToString:districtIdStr]) {
                    [cinemas addObject:cinemaModel];
                }
            }
            
            [districts addObject:cinemas];
            
    
        }
        
        _allCinemasAr = districts;
        
    }

    return _allCinemasAr;
}

#pragma mark - 加载列表

- (void)loadTabelView{
    
    _mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight - 64) style:UITableViewStylePlain];
    _mTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main@2x"]];
    _mTableView.dataSource = self;
    _mTableView.delegate = self;
    _mTableView.separatorColor = [UIColor greenColor];
    
    [self.view addSubview:_mTableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.allCinemasAr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSNumber *sectionNumber = @(section);
    if ([_closedAr containsObject:sectionNumber]) {
        return 0;
    }
    
    NSArray *array = self.allCinemasAr[section];
    return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWCinemaCell *cell = [JWCinemaCell cinemaCellWithTableView:tableView];
    NSArray *array = self.allCinemasAr[indexPath.section];
    cell.cinemaModel = array[indexPath.row];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


#pragma mark - 组头视图设置

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    JWDistrictModel *model = self.districtAr[section];
    NSString *name = model.name;
    
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, 40)];
    headerview.backgroundColor = [UIColor clearColor];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:headerview.frame];
    bgImageView.image = [UIImage imageNamed:@"hotMovieBottomImage@2x"];
    [headerview addSubview:bgImageView];
    
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, JWScreenWidth - 10, 40)];
    headerLabel.text = name;
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.backgroundColor = [UIColor clearColor];
    
    UIButton *headerBtn = [[UIButton alloc] initWithFrame:headerview.frame];
    headerBtn.tag = section;
    headerBtn.backgroundColor = [UIColor clearColor];
    [headerBtn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [headerview addSubview:headerLabel];
    [headerview addSubview:headerBtn];
    
    return headerview;
  
}

- (void)headerBtnAction:(UIButton *)btn{
    
    NSNumber *tagNumber = @(btn.tag);
    
    if ([self.closedAr containsObject:tagNumber]) {
        [self.closedAr removeObject:tagNumber];
    }else {
        [self.closedAr addObject:tagNumber];
    }
    
    [self.mTableView reloadData];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
